import env from './env.js';

const baseUrl = env.baseUrl
export const request = (options) => {
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		uni.request({
			url: baseUrl + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: options.header || {
				Authorization: token,
			},
			success: (res) => {
				const data = res.data
				// console.log(data, "data");
				const userLoginInfo = uni.getStorageSync('token');
				if (userLoginInfo) {
					
				} else if (!userLoginInfo) {
					console.log('没有登录过');
					uni.navigateTo({
						url: '/pages/logo/logo'
					})
				} else if (data.code == 401) {
					// console.log('认证失败');
					uni.navigateTo({
						url: '/pages/logo/logo'
					})
				} else if (data.code !== 200) {
					// console.log(data.code);
					uni.showToast({
						icon: 'error',
						title: '操作错误'
					})
					uni.navigateTo({
						url: '/pages/logo/logo'
					})
				}
				resolve(data)
			},
			fail: (error) => {
				uni.showToast({
					icon: 'error',
					title: '系统错误'
				})
				reject(error)
			}
		})
	})
}